package com.milotnt.controller;

import com.milotnt.pojo.DeviceEntity;
import com.milotnt.service.DeviceService;
import com.milotnt.service.ProductService;
import com.milotnt.vo.ProductVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private ProductService productService;

    //查询器材
    @RequestMapping("/selDevice")
    public String selectDevice(Model model, HttpSession session) {
        List<DeviceEntity> deviceList = deviceService.findAll();
        session.setAttribute("deviceList", deviceList);
        model.addAttribute("deviceList", deviceList);
        return "selectDevice";
    }

    //删除器材
    @RequestMapping("/delDevice")
    public String deleteDevice(Long deviceId) {
        deviceService.deleteByDeviceId(deviceId);
        return "redirect:selDevice";
    }

    //跳转修改器材页面
    @RequestMapping("/toUpdateDevice")
    public String toUpdateDevice(Long deviceId, Model model) {
        List<DeviceEntity> deviceList = deviceService.selectByDeviceId(deviceId);
        model.addAttribute("deviceList", deviceList);
        return "updateDevice";
    }

    //修改器材
    @RequestMapping("/updateDevice")
    public String updateDevice(DeviceEntity device) {
        deviceService.updateDeviceByDeviceId(device);
        return "redirect:selDevice";
    }

    //跳转新增器材页面
    @RequestMapping("/toAddDevice")
    public String toAddDevice(Model model) {
        // 获取所有产品列表
        List<ProductVo> productList = productService.findAllProducts();
        model.addAttribute("productList", productList);
        return "addDevice";
    }

    //新增器材
    @RequestMapping("/addDevice")
    public String addDevice(DeviceEntity device) {
        deviceService.insertDevice(device);
        return "redirect:selDevice";
    }

}
